/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.ly.wxstore.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ly.wxstore.comm.DateUtil;
import com.ly.wxstore.entity.MarketingAccount;
import com.ly.wxstore.entity.SalerDownHierarchy;
import com.ly.wxstore.entity.weixin.WeixinUserInfo;
import com.ly.wxstore.service.MarketingAccountService;
import com.ly.wxstore.service.SalerDownHierarchyService;
import com.ly.wxstore.service.account.ShiroDbRealm.ShiroUser;
import com.ly.wxstore.service.weixin.WeixinConf;
import com.ly.wxstore.service.weixin.WeixinUserInfoService;
import com.ly.wxstore.vo.page.MyCenterPageVo;

/**
 * 管理员管理用户的Controller.
 * 
 * @author peter
 */
@Controller
@RequestMapping(value = "/MyCenter")
public class MyCenterController {

	private static Logger logger = LoggerFactory.getLogger(MyCenterController.class);

	@Autowired
	private WeixinUserInfoService weixinUserInfoService;
	@Autowired
	private SalerDownHierarchyService salerDownHierarchyService;
	@Autowired
	private MarketingAccountService marketingAccountService;

	@Autowired
	private WeixinConf weixinConf;

	/**
	 * 进入个人中心页面
	 * 
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getMyCenterPage(HttpServletResponse response, Model model) {
		logger.info("MyCenter");

		model.addAttribute("weixinUserInfo", "testUserInfo");
		return "MyCenter";
	}

	@RequestMapping(value = "userInfolist.json", method = RequestMethod.GET)
	@ResponseBody
	public MyCenterPageVo list() {
		String openid=getCurrentUser().getOpenid();
		
		MyCenterPageVo vo = new MyCenterPageVo();

		// 微信用户信息
		WeixinUserInfo weixinUserInfo = weixinUserInfoService.getByOpenIdAndWeixinPublicId(openid, weixinConf.getPublicId());
		vo.setNickname(weixinUserInfo.getNicknameForBase64Decoder());
		vo.setImageUrl(weixinUserInfo.getHeadimgurl());

		return vo;
	}

	/**
	 * 使用Ehcache缓存 ShiroUse 信息，如果缓存失效，user里面的数据将会丢失
	 * 
	 * @return
	 */
	private ShiroUser getCurrentUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}

}
